require "socket"

Thread.abort_on_exception = true

gs = TCPserver.open(8080)
addr = gs.addr
addr.shift            # removes "AF_INET"
printf("server is on %s\n", addr.join(":"))

begin

trap("SIGINT") {
	gs.close
	exit 0
}

def receive_request soc, request
	req = request.match(/^GET (.*) HTTP\/1.1/)[1]
	
	case req
	when '/'
		soc.puts "a"
	end
	
	soc.close_write
end

loop do; Thread.start(gs.accept) do |c|
begin
	while req = c.gets and req.chomp!
		if req =~ /^GET / then
			receive_request c, req
		end
	end
ensure
	c.close if c and not c.closed?
end
end; end

ensure
	gs.close if gs and not gs.closed?
end